package com.example.mongodemo.controller;

import com.example.mongodemo.bean.Location;
import com.example.mongodemo.bean.Person;
import com.example.mongodemo.dao.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author zcx
 * @Title 测试mogoDb控制器
 * @date 2019年01月16日 11:45
 **/
@RestController
public class DataController {

    @Autowired
    PersonRepository repository;

    /**
    *将数据保存到mongoDb中
     * @author zcx
     * @date 2019/1/16 13:55
     * @param
     * @return com.example.mongodemo.bean.Person
     */
    @RequestMapping(value = "/save")
    public Person save() {
        Person person = new Person("zcx",26);
        Collection<Location> locations = new LinkedHashSet<Location>();
        Location location = new Location("福州软件园","2015");
        Location location1 = new Location("福州高新区","2016");
        Location location2 = new Location("福州城门","2017");
        Location location3 = new Location("福州仓山","2018");
        Location location4 = new Location("福州软件园文山里","2018");
        locations.add(location);
        locations.add(location1);
        locations.add(location2);
        locations.add(location3);
        locations.add(location4);
        person.setLocations(locations);
        return repository.save(person);
    }

    /*
    *根据姓名查找信息
     * @author zcx
     * @date 2019/1/16 14:07
     * @param name
     * @return com.example.mongodemo.bean.Person
     */
    @RequestMapping(value = "/name")
    public Person queryName(String name) {
        return repository.findByName(name);
    }

    /*
    *根据年份查找信息
     * @author zcx
     * @date 2019/1/16 14:09
     * @param age
     * @return java.util.List<com.example.mongodemo.bean.Person>
     */
    @RequestMapping(value = "/year")
    public List<Person> queryYearInfo(Integer age) {
         return repository.withQueryFindByAge(age);
    }
}
